$(function () {
    var tcName = $("#userName").val();
    if (tcName != "") {
        $("#refundPeople option[value='" + tcName + "']").attr("selected", "selected");
    }
    if ($("#refundPeople").val() != '') {
        $("#refundPeople").attr('disabled', true).css('background', '#ebebe4');
    }
    var myDate = new Date();
    var year = myDate.getFullYear();
    var month = myDate.getMonth() + 1;
    var date = myDate.getDate();
    var h = myDate.getHours();
    var m = myDate.getMinutes();
    var s = myDate.getSeconds();
    var now = year + '-' + getNow(month) + "-" + getNow(date) + " " + getNow(h) + ':' + getNow(m) + ":" + getNow(s);
    $("#refundTime").val(now);
});

function getNow(s) {
    return s < 10 ? '0' + s : s;
}

var refundcount = 0;

function closesd() {
    refundcount = 0;
    parent.layer.close(parent.layer.getFrameIndex(window.name));
}

function retreatAllOrder(obj) {  //退整单
    refundcount++;
    if (refundcount == 1) {
        $.ajax({
            url: Feng.ctxPath + "/consultant/getCustomerStatus/" + $("#customerId").val() ,
            dataType: "json",
            contentType: "application/json",
            type: "POST",
            success: function (data) {
                if (data.split(",")[0] == '复诊') {
                    alert("复诊不能退款！请重咨");
                    refundcount = 0;
                    return false;
                } else if (data.split(",")[1] > 0) {
                    alert("此患者还有未结账的单，不能退款！");
                    refundcount = 0;
                    return false;
                } else {
                    var backOrders = $(".backOrders1");
                    var dataList = [];
                    var getTime = $("#refundTime").val();
                    var refundPeople = $("#refundPeople").val();
                    if (getTime == null || getTime == "") {
                        alert("请选择退款时间");
                        refundcount = 0;
                        return false;
                    }
                    if (refundPeople == null || refundPeople == "") {
                        alert("请选择退款人");
                        refundcount = 0;
                        return false;
                    }
                    var remarks = $("#remarks").val();
                    if (remarks == null || remarks == "") {
                        alert("请输入退款备注！");
                        refundcount = 0;
                        return false;
                    }
                    for (var i = 0; i < backOrders.length; i++) {
                        var obj = backOrders[i];
                        var deal = {};
                        deal.id2 = $(obj).find(".id2").val();
                        deal.deductionTypeName = $("#deductionTypeName").val();
                        deal.referralerPerson = $("#referralerPerson").val();
                        deal.referralerPersoned = $("#referralerPersoned").val();
                        deal.referralerTime = $("#referralerTime").val();
                        deal.isDo = 4;
                        deal.customerId = $("#customerId").val();
                        deal.reservationsId = $("#reservationsId").val();
                        deal.dealStatus = "已开单";
                        deal.tcName = refundPeople;
                        deal.payServiceNum = $(obj).find(".payServiceNum").html();
                        deal.payService = $(obj).find(".payService").html();
                        deal.foldingPay = $(obj).find(".foldingPay").html();
                        deal.serviceActualPay = $(obj).find(".serviceActualPay").html();
                        deal.deductionAmount = $(obj).find(".integrals").html();
                        deal.arrears = $(obj).find(".arrears").val();
                        deal.payServiceNumST = $(obj).find(".payServiceNumST").val();  //退项目数量
                        deal.remarks = remarks;
                        dataList.push(deal);
                    }
                    $.ajax({
                        url: Feng.ctxPath + "/consultant/backAllOrder",
                        dataType: "json",
                        contentType: "application/json",
                        data: JSON.stringify(dataList),
                        type: "POST",
                        success: function (data) {
                            $(obj).attr("disabled", "true"); //设置变灰按钮
                            layer.msg('操作成功！', {time: 1000, icon: 1});
                            window.parent.location.href = Feng.ctxPath + "/consultantDetails/consultantToDetailByTab/" + $("#customerId").val() + '/3/0'
                        }
                    });
                }
            }
        })
    }
}